package top.lixunda.ecommerce.server.good.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.lixunda.common.utils.ObjectValidators;
import top.lixunda.ecommerce.server.business.BaseServiceImpl;
import top.lixunda.ecommerce.server.business.api.entity.BusinessConstants;
import top.lixunda.ecommerce.server.good.api.entity.db.SysGoodActivityDO;
import top.lixunda.ecommerce.server.business.api.entity.dto.PageDTO;
import top.lixunda.ecommerce.server.good.mapper.SysGoodActivityMapper;
import top.lixunda.ecommerce.server.good.service.ISysGoodActivityService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 商品活动，由管理员管理，商家自动选择加入 服务实现类
 * </p>
 *
 * @author Xumda
 * @since 2019-12-18
 */
@Service
public class SysGoodActivityServiceImpl
        extends BaseServiceImpl<SysGoodActivityMapper, SysGoodActivityDO>
        implements ISysGoodActivityService {

    @Autowired
    public SysGoodActivityServiceImpl() {
    }

    /**
     * 根据id查询活动信息，可以获取到过期的活动信息
     *
     * @param id 活动id
     * @return 活动信息
     */
    @Override
    public SysGoodActivityDO queryById(Integer id) {
        if (ObjectValidators.isEmpty(id)) {
            throw getAppException("id不能为空");
        }
        return this.getById(String.valueOf(id));
    }

    /**
     * 获取所有可用的活动信息，没有过期，正常状态
     *
     * @return 活动信息列表
     */
    @Override
    public List<SysGoodActivityDO> queryUseableActivities() {
        LocalDateTime time = LocalDateTime.now();
        return this.list(
                new LambdaQueryWrapper<SysGoodActivityDO>()
                        .eq(SysGoodActivityDO::getUseable, BusinessConstants.USEABLE_TRUE)
                        .le(SysGoodActivityDO::getStartTime, time)
                        .ge(SysGoodActivityDO::getEndTime, time)
                        .eq(SysGoodActivityDO::getState, SysGoodActivityDO.STATE_NORMAL));
    }

    /**
     * 分页获取所有可用的活动信息，没有过期，正常状态
     *
     * @param pageDTO 分页数据
     * @return 分页的系统活动数据
     */
    @Override
    public IPage<SysGoodActivityDO> queryUseableActivitiesPage(PageDTO<SysGoodActivityDO, ?> pageDTO) {
        if (pageDTO == null) {
            throw getAppException("分页数据不能为空");
        }
        return this.page(pageDTO.getPage());
    }

}
